'use client'; import { useState, useEffect, useRef } from 'react'; import Link from 'next/link'; import { useParams } from 'next/navigation'; import { getConflicts, getPeopleByConflict, Conflict, PersonDetail } from '@/lib/api'; import Header from '@/components/Header'; import DocumentIcon from '@/components/DocumentIcon'; import AwardIcon from '@/components/AwardIcon'; import Pagination from '@/components/Pagination'; export default function ConflictPage() { const params = useParams(); const conflictId = parseInt(params.id as string); const [conflict, setConflict] = useState(null); const [people, setPeople] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [currentPage, setCurrentPage] = useState(1); const [itemsPerPage, setItemsPerPage] = useState(30); // Ref for scrolling to top of results list const resultsRef = useRef(null); useEffect(() => { async function fetchData() { try { const conflicts = await getConflicts(); const currentConflict = conflicts.find(c => c.id === conflictId); if (!currentConflict) { throw new Error('Conflict not found'); } setConflict(currentConflict); const peopleData = await getPeopleByConflict(conflictId); setPeople(peopleData); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load data'); console.error(err); } finally { setLoading(false); } } fetchData(); }, [conflictId]); // Calculate paginated data const totalItems = people.length; const startIndex = (currentPage - 1) * itemsPerPage; const endIndex = startIndex + itemsPerPage; const paginatedPeople = people.slice(startIndex, endIndex); // Handlers for pagination const handlePageChange = (page: number) => { setCurrentPage(page); // Scroll to top of results list resultsRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' }); }; const handleItemsPerPageChange = (newItemsPerPage: number) => { setItemsPerPage(newItemsPerPage); setCurrentPage(1); // Reset to first page when changing items per page }; if (loading) { return (

Loading...

); } if (error || !conflict) { return (

{error || 'Conflict not found'}

Return to Home
); } return (
{/* Main Content */}
{/* Conflict Header */}

{conflict.name}

{conflict.start_year === conflict.end_year ? conflict.start_year : `${conflict.start_year} – ${conflict.end_year || 'Present'}`}

{conflict.description && (

{conflict.description}

)}

{conflict.casualty_count} VMI Alumni Gave Their Lives

{/* People List */}

Honor Roll

{people.length === 0 ? (

No casualties recorded yet.

) : ( <> {/* Pagination Controls - Top */} {/* People Grid */}
{paginatedPeople.map((person) => (

{(() => { const name = person.full_display_name || person.display_name; // Only remove rank if it exists if (person.rank) { return name.replace(person.rank + ' ', '').replace(person.rank + ', ', ''); } return name; })()} {person.has_awards && } {person.pdf_key && }

{person.rank && (

{person.rank}

)} {person.unit && (

{person.unit}

)} {person.death_description && (

{person.death_description}

)} ))}
{/* Pagination Controls - Bottom */} )}
); }